/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.api.news.controller.NewsController.java
 * <p>
 * 类产生时间: 14-1-24 下午12:00
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.news.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.FileUtils;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.channel.entity.Channel;
import com.oceansoft.mobile.econsole.modules.news.entity.News;
import com.oceansoft.mobile.econsole.modules.news.service.INewsService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 新闻控制器
 *
 * @author: chenw
 * @time: 14-1-24 下午12:00
 */
@Controller
@RequestMapping("news")
public class NewsController extends BaseController {

    @Resource
    private INewsService newsService;

    /**
     * 新闻列表
     *
     * @param model 新闻列表
     * @return String
     */
    @RequestMapping(value = "index")
    public String gotoChannelIndex(Query query, Model model) {
        Pager<News> pager = newsService.loadAllNewsByPage(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log(String.format("查看新闻栏目列表第%d页", query.getPageNum()));
        return "news/channel-list";
    }


    
    /**
     * 删除某条新闻
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete/{newsId}")
    public Result deleteNews(@PathVariable("newsId")int newsId) {
    	return newsService.deleteNews(newsId);
    }
    
    /**
     * 发布新闻
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "pubNews/{newsId}")
    public Result pubNews(@PathVariable("newsId")int newsId) {
    	return newsService.pubNews(newsId);
    }
    
    /**
     * 查看某条新闻
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "newsDetail/{news_id}", method = GET)
    public Result loadNewsDetail(@PathVariable("news_id") long newsId) {
        return newsService.loadNewsById(newsId);
    }
    
    /**
     * 跳转到新增编辑页面
     * @param newsId
     * @param model
     * @return
     */
    @RequestMapping(value = "gotoNews/{newsId}")
    public String gotoNews(@PathVariable("newsId")long newsId, Model model) {
    	List<Channel> channels = newsService.loadAllChannels();
    	model.addAttribute("channels", channels);
    	if(newsId>0){
    		//查看已有新闻并编辑
    		News news = (News) newsService.loadNewsById(newsId).getData();
    		model.addAttribute("news", news);
    		model.addAttribute("isAddNews", 1);
    	}else{
    		//新增新闻界面
    		model.addAttribute("isAddNews", 0);
    	}
    	model.addAttribute("flag", new Date().getTime());
    	return "news/news-edit2";
    }
    
    /**
     * 预览新闻
     * @param newsId
     * @param model
     * @return
     */
    @RequestMapping(value = "viewNews/{newsId}")
    public String viewNews(@PathVariable("newsId")long newsId, Model model) {
    	model.addAttribute("newId", newsId);
    	return "news/news-details";
    }
    
    /**
     * 保存或修改新闻新闻
     * @param news
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "addNews",method = POST)
    public Result addNews(News news, MultipartHttpServletRequest request) throws Exception{
    	int isAddNews = Integer.parseInt(request.getParameter("isAddNews"));
    	MultipartFile mpf = request.getFile("photo");
    	if(mpf != null){
    		String newFileName = String.format("%s%s", DigestUtils.md5DigestAsHex(mpf.getBytes()), FileUtils.getExtName(mpf.getOriginalFilename()));
    		String photoUrl = String.format("%s/%s", configInfo.getFileUploadRootPath(), "news");
    		//存储文件至文件系统
    		mpf.transferTo(FileUtils.createFile(new File(photoUrl), newFileName));
    		news.setImgUrl(configInfo.getFileDownloadUrl("news/"+newFileName));
    	}
    	if(isAddNews == 0){
    		if( "0".equals(news.getPubDept())){
    			news.setPubDept("app后台");
    		}
    		if("1".equals(news.getPubDept())){
    			news.setPubDept("app后台_V2");
    		}
    	//	news.setPubDept("app后台_V2");
            newsService.saveNewsBeforePub(news);
    		return new Result(Result.SUCC,"保存成功");
    	}else{
    		if( "0".equals(news.getPubDept())){
    			news.setPubDept("app后台");
    		}
    		if("1".equals(news.getPubDept())){
    			news.setPubDept("app后台_V2");
    		}
    		return newsService.updateNews(news);
    	}
    }
    
    /**
     * 新闻图片上传
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "editorUpload")
    public String editUpload(MultipartHttpServletRequest request) throws Exception{
    	return newsService.editorUpload(request);
    }
    
}
